package com.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.common.exception.BizCodeEnum;
import com.common.to.OAuthResTo;
import com.common.to.UserLoginTo;
import com.common.to.UserRegTo;
import com.gulimall.member.exception.PhoneExistException;
import com.gulimall.member.exception.UserNameExistException;
import com.gulimall.member.service.impl.MemberServiceImpl;
import org.springframework.web.bind.annotation.*;

import com.gulimall.member.entity.MemberEntity;
import com.common.utils.PageUtils;
import com.common.utils.R;

import javax.annotation.Resource;


/**
 * 会员
 *
 * @author Rookie-886
 * @email ${email}
 * @date 2021-03-14 22:25:33
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Resource
    private MemberServiceImpl memberServiceImpl;


    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberServiceImpl.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 登陆功能
     * @param userRegTo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody UserLoginTo userRegTo){
        MemberEntity memberEntity = memberServiceImpl.login(userRegTo);
        if(memberEntity == null){
            return R.error(BizCodeEnum.USER_NOTFOUND_EXCEPTION.getCode(), BizCodeEnum.USER_NOTFOUND_EXCEPTION.getMsg());
        }else{
            return R.ok().setData(memberEntity);
        }
    }

    /**
     * OAuth2.0 登陆&注册功能
     * @param oAuthResTo
     * @return
     */
    @PostMapping("/OAuth/login")
    public R OAuth(@RequestBody OAuthResTo oAuthResTo){
        MemberEntity memberEntity = memberServiceImpl.oAuthLogin(oAuthResTo);
        return R.ok().setData(memberEntity);
    }

    /**
     * 注册功能
     * @param userRegTo
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody UserRegTo userRegTo){
        try {
            memberServiceImpl.register(userRegTo);
        } catch (PhoneExistException e) {
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
        } catch (UserNameExistException e){
            return R.error(BizCodeEnum.USERNAME_EXIST_EXCEPTION.getCode(), BizCodeEnum.USERNAME_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberServiceImpl.getById(id);

        return R.ok().put("member", member);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
        memberServiceImpl.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
        memberServiceImpl.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
        memberServiceImpl.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
